package com.bgrc.hubu12305.controller;

import com.bgrc.hubu12305.common.Result;
import com.bgrc.hubu12305.entity.TrainInfoEntity;
import com.bgrc.hubu12305.service.TrainQueryService;
import com.bgrc.hubu12305.service.TrainService;
import com.bgrc.hubu12305.service.impl.TrainQueryServiceImpl;
import com.bgrc.hubu12305.vo.TrainQueryReturnVO;
import com.bgrc.hubu12305.vo.TrainReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 包名:com.bgrc.hubu12305.controller
 * 用户:86182
 * 时间:20211217:592021/12/15
 */
@RestController
@RequestMapping("train")
@Api(tags = "车次查询接口")
public class TrainController {

    @Autowired
    private TrainService trainService;

    @Autowired
    private TrainQueryService trainQueryService;

    //首页显示所有车次信息
    @ApiOperation(value = "首页显示所有车次信息,分页")
    @GetMapping("/queryAllTrainPage")
    public Result queryAllTrainPage(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage){

        List<TrainInfoEntity> allTrain = trainService.getAllTrainPage(currentPage);

        List<TrainReturnVO> returnVOListPage = new ArrayList<>();
        for (TrainInfoEntity train:allTrain
        ) {
            Integer runningTime = trainService.getRunningTime(train.getTrainNumber());
            TrainReturnVO trainReturnVO = new TrainReturnVO(train.getTrainNumber(),train.getTrainNo(),train.getTrainStartStation(),train.getTrainEndStation(),train.getTrainStartTime()
                    ,train.getTrainEndTime(),runningTime);
            returnVOListPage.add(trainReturnVO);
        }
        return Result.ok(returnVOListPage);
    }

    //首页显示所有车次信息
    @ApiOperation(value = "首页显示所有车次信息,不分页")
    @GetMapping("/queryAllTrainNoPage")
    public Result queryAllTrainNoPage(){

        List<TrainInfoEntity> allTrain = trainService.getAllTrain();

        List<TrainReturnVO> returnVOList = new ArrayList<>();
        for (TrainInfoEntity train:allTrain
        ) {
            Integer runningTime = trainService.getRunningTime(train.getTrainNumber());
            TrainReturnVO trainReturnVO = new TrainReturnVO(train.getTrainNumber(),train.getTrainNo(),train.getTrainStartStation(),train.getTrainEndStation(),train.getTrainStartTime()
                    ,train.getTrainEndTime(),runningTime);
            returnVOList.add(trainReturnVO);
        }
        return Result.ok(returnVOList);
    }


    @ApiOperation(value = "通过出发和目的站查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "startStation",value = "出发站",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "endStation",value = "到达站",paramType = "query",dataType = "String")} )
    @GetMapping("/queryByStation")
    public Result queryByStation(@RequestParam String startStation,@RequestParam String endStation){

        System.out.println("start:"+startStation);
        System.out.println("end:"+endStation);
        List<TrainQueryReturnVO> returnVOList = trainQueryService.queryTrainList(startStation, endStation);
        return Result.ok(returnVOList);

    }

}
